/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "iosdrv.h"

#include <string.h>

#include "atom.h"
#include "stm8l15x_gpio.h"
#include "pin.h"
#include "drv_pin.h"

#define __STM8_PIN(index, gpio, gpio_index)                                \
    {                                                                       \
        index, GPIO##gpio, GPIO_Pin_##gpio_index                            \
    }

static const struct pin_index pins[] = 
{
    __STM8_PIN(0 ,  A, 0 ),
    __STM8_PIN(1 ,  A, 1 ),
    __STM8_PIN(2 ,  A, 2 ),
    __STM8_PIN(3 ,  A, 3 ),
    __STM8_PIN(4 ,  A, 4 ),
    __STM8_PIN(5 ,  A, 5 ),
    __STM8_PIN(6 ,  A, 6 ),
    __STM8_PIN(7 ,  A, 7 ),
    __STM8_PIN(8 ,  B, 0 ),
    __STM8_PIN(9 ,  B, 1 ),
    __STM8_PIN(10,  B, 2 ),
    __STM8_PIN(11,  B, 3 ),
    __STM8_PIN(12,  B, 4 ),
    __STM8_PIN(13,  B, 5 ),
    __STM8_PIN(14,  B, 6 ),
    __STM8_PIN(15,  B, 7 ),
    __STM8_PIN(16,  C, 0 ),
    __STM8_PIN(17,  C, 1 ),
    __STM8_PIN(18,  C, 2 ),
    __STM8_PIN(19,  C, 3 ),
    __STM8_PIN(20,  C, 4 ),
    __STM8_PIN(21,  C, 5 ),
    __STM8_PIN(22,  C, 6 ),
    __STM8_PIN(23,  C, 7 ),
    __STM8_PIN(24,  D, 0 ),
    __STM8_PIN(25,  D, 1 ),
    __STM8_PIN(26,  D, 2 ),
    __STM8_PIN(27,  D, 3 ),
    __STM8_PIN(28,  D, 4 ),
    __STM8_PIN(29,  D, 5 ),
    __STM8_PIN(30,  D, 6 ),
    __STM8_PIN(31,  D, 7 ),
    __STM8_PIN(32,  E, 0 ),
    __STM8_PIN(33,  E, 1 ),
    __STM8_PIN(34,  E, 2 ),
    __STM8_PIN(35,  E, 3 ),
    __STM8_PIN(36,  E, 4 ),
    __STM8_PIN(37,  E, 5 ),
    __STM8_PIN(38,  E, 6 ),
    __STM8_PIN(39,  E, 7 ),
    __STM8_PIN(40,  F, 0 ),
    __STM8_PIN(41,  F, 1 ),
    __STM8_PIN(42,  F, 2 ),
    __STM8_PIN(43,  F, 3 ),
    __STM8_PIN(44,  F, 4 ),
    __STM8_PIN(45,  F, 5 ),
    __STM8_PIN(46,  F, 6 ),
    __STM8_PIN(47,  F, 7 ),
    __STM8_PIN(48,  G, 0 ),
    __STM8_PIN(49,  G, 1 ),
    __STM8_PIN(50,  G, 2 ),
    __STM8_PIN(51,  G, 3 ),
    __STM8_PIN(52,  G, 4 ),
    __STM8_PIN(53,  G, 5 ),
    __STM8_PIN(54,  G, 6 ),
    __STM8_PIN(55,  G, 7 ),
};

    
#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
const struct pin_index *get_pin(uint8_t pin)
{
    const struct pin_index *index;

    if (pin < ITEM_NUM(pins))
    {
        index = &pins[pin];
        if (index->index == -1)
            index = NULL;
    }
    else
    {
        index = NULL;
    }

    return index;
};


int  stm8l_read( const Pin* dev, int pin )
{
    int value;
    const struct pin_index *index;

    value = PIN_LOW;

    index = get_pin(pin);
    if (index == NULL)
    {
        return value;
    }

    value = GPIO_ReadInputDataBit(index->gpio, index->pin);
    
    if(value > 0) return HIGH;
    else return LOW;
}


void stm8l_write( const Pin* dev, int pin, int value )
{
    const struct pin_index *index;

    index = get_pin(pin);
    if (index == NULL)
    {
        return;
    }

    GPIO_WriteBit(index->gpio, index->pin, (BitAction)value);
}

void stm8l_toggle( const Pin* dev, int pin)
{
    const struct pin_index *index;

    index = get_pin(pin);
    if (index == NULL)
    {
        return;
    }

    GPIO_ToggleBits(index->gpio, index->pin);
}

void stm8l_mode( const Pin* dev, int pin, int mode)
{
    const struct pin_index *index;
    GPIO_Mode_TypeDef mode_type;

    index = get_pin(pin);
    if (index == NULL)
    {
        return;
    }

    if (mode == PIN_MODE_OUTPUT)
    {
        /* output setting */
        mode_type = GPIO_Mode_Out_PP_High_Fast;
    }
    else if (mode == PIN_MODE_INPUT)
    {
        /* input setting: not pull. */
        mode_type = GPIO_Mode_In_FL_No_IT;
    }
    else if (mode == PIN_MODE_INPUT_PULLUP)
    {
        /* input setting: pull up. */
        mode_type = GPIO_Mode_In_PU_No_IT;

    }
    else if (mode == PIN_MODE_INPUT_PULLDOWN)
    {
        /* input setting: pull down. */
        mode_type = GPIO_Mode_In_FL_No_IT;
    }

    else if (mode == PIN_MODE_OUTPUT_OD)
    {
        /* output setting: od. */
        mode_type = GPIO_Mode_Out_OD_HiZ_Fast;
    }

    GPIO_Init(index->gpio, index->pin, mode_type);
}


static struct PinOps _pin_ops = {
    stm8l_read,
    stm8l_write,
    stm8l_toggle,
    stm8l_mode
};

void Stm8lPinCreate()
{
    PinCreate("pin", &_pin_ops);
}